import './assets/main.css'

import { createApp } from 'vue'
import { createPinia } from 'pinia'
import App from './App.vue'
import router, { setupDynamicRoutes } from './router'
import ElementPlus from 'element-plus'
import 'element-plus/dist/index.css'
import zhCn from 'element-plus/dist/locale/zh-cn.mjs'

// 导入Element Plus图标
import * as ElementPlusIconsVue from '@element-plus/icons-vue'

const app = createApp(App)

// 覆盖 Element Plus 的控制台警告函数
const originalWarn = console.warn
console.warn = function(...args) {
  if (typeof args[0] === 'string' &&
     (args[0].includes('[ElPagination]') ||
      args[0].includes('废弃的用法') ||
      args[0].includes('ElementPlusError'))) {
    return
  }
  return originalWarn.apply(console, args)
}

// 禁用特定警告
app.config.warnHandler = (msg) => {
  if (msg.includes('[ElPagination]') ||
      msg.includes('废弃的用法') ||
      msg.includes('ElementPlusError')) {
    return
  }
  console.warn(msg)
}

// 异步初始化应用
async function initApp() {
  try {
    console.log("初始化应用...")

    // 创建Pinia实例
    const pinia = createPinia()
    app.use(pinia)

    // 初始化路由
    app.use(router)

    // 初始化ElementPlus
    app.use(ElementPlus, {
      locale: zhCn,
      silent: true // 禁用控制台警告
    })

    // 注册所有图标
    for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
      app.component(key, component)
    }

    // 挂载应用
    app.mount('#app')

    // 在应用初始化完成后设置动态路由
    console.log("应用已挂载，正在设置动态路由...")
    await setupDynamicRoutes()
    console.log("动态路由设置完成")

  } catch (error) {
    console.error("应用初始化失败:", error)
  }
}

// 启动应用
initApp()
